package com.bit.dreamer;

import java.util.ArrayList;

import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.style.ClickableSpan;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class MainMenuActivity extends Activity implements OnClickListener{

	Button btn_mento, btn_dodo, btn_have, btn_become;
	Intent intent;
	SharedPreferences sf;
	DreamHelper helper;
	DreamMainMenuAdapter adapter;
	private ListView listView;
	private SQLiteDatabase db;
	private Cursor rs;
	private ArrayList<ListContent> list;
	 int dolv = R.id.dodolv;
		int havelv = R.id.havelv;
		int becomelv = R.id.becomelv;

		String dodo="SELECT title, dreamdate FROM D_do_tb " +
				"ORDER BY dreamdate ASC,title ASC;";
		String have="SELECT title, dreamdate FROM D_have_tb " +
				"ORDER BY dreamdate ASC,title ASC;";
		String become="SELECT title, dreamdate FROM D_become_tb " +
				"ORDER BY dreamdate  ASC,title ASC;";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_main_menu);
		
		
		
		ActionBar actionbar = getActionBar();
		sf = getSharedPreferences("name", MODE_PRIVATE);
		String nName = sf.getString("name", "");
		actionbar.setTitle(nName + "님 안녕하세요");
		actionbar.setDisplayShowHomeEnabled(false);


		btn_mento=(Button) findViewById(R.id.mento);
		btn_dodo=(Button) findViewById(R.id.dodo);
		btn_have=(Button) findViewById(R.id.have);
		btn_become=(Button) findViewById(R.id.become);

		btn_mento.setOnClickListener(this);
		btn_dodo.setOnClickListener(this);
		btn_have.setOnClickListener(this);
		btn_become.setOnClickListener(this);

			   

		sqlhelp(dodo, dolv);
		sqlhelp(have, havelv);
		sqlhelp(become,becomelv);
		
				
	
		
	}
	
	protected void onResume() {
	    super.onResume();
	    sqlhelp(dodo, dolv);
		sqlhelp(have, havelv);
		sqlhelp(become,becomelv);
	}

	
	
	public void sqlhelp(String sql, int dolv) {
		helper = new DreamHelper(this);

		list= new ArrayList<ListContent>();
		adapter = new DreamMainMenuAdapter(this, R.layout.dream_list , list);

		listView = (ListView)findViewById(dolv);
		listView.setAdapter(adapter);
		listView.setSelector(R.drawable.list_selector);
		db = helper.getReadableDatabase();

		rs = db.rawQuery(sql, null);
		while (rs.moveToNext()) {
			list.add(new ListContent(rs.getString(0), rs.getString(1)));
		}//while

		adapter.notifyDataSetChanged();

		rs.close();
		helper.close();

	}
	
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.mento:
			intent = new Intent(getApplicationContext(), 
					com.bit.dreamer.SubMenu.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			break;
			
		case R.id.dodo :
			
			intent = new Intent(getApplicationContext(), 
					DreamlistActivity.class);
			intent.putExtra("bt_select", 1);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			break;
			
		case R.id.have :
			
			intent = new Intent(getApplicationContext(), 
					DreamlistActivity.class);
			intent.putExtra("bt_select", 2);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			break;
			
		case R.id.become :
			
			intent = new Intent(getApplicationContext(), 
					DreamlistActivity.class);
			intent.putExtra("bt_select", 3);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			break;
		}


	}
	
	//Back버튼 눌렸을때 ....
	public void onBackPressed(){

		AlertDialog.Builder exit = new AlertDialog.Builder(this).setMessage("정말 종료하시겠습니까?");

		exit.setPositiveButton("예", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				moveTaskToBack(true);
				finish();
				dialog.cancel();
			}
		});

		exit.setNegativeButton("아니요", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
			}
		});
		exit.show();
	} 

	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		
		// Inflate the menu; this adds items to the action bar if it is present.
//		getMenuInflater().inflate(R.menu.lotto_main, menu);
//		return true;
		
		super.onCreateOptionsMenu(menu);
		
		MenuItem item = menu.add(0, 1, 0, "로그인");
		item.setIcon(R.drawable.ic_launcher);
		item.setAlphabeticShortcut('a'); // 키보드 a를 누르면 실행되게끔

		menu.add(0, 2, 0, "꿈등록");
		menu.add(0, 3, 0, "꿈지도");
		menu.add(0, 4, 0, "메뉴들");
		
		//menu.add(0, 5, 0, "메뉴들"); // 축소로 주석
		//menu.add(0, 6, 0, "심리");
		//menu.add(0, 7, 0, "DreamMap");


		/* 기타 서브메뉴 없어서 보류. 주석
	     SubMenu etc = menu.addSubMenu("기타");
	     etc.add(0, 4, 0, "참고1");
	     etc.add(0, 5, 0, "참고2");
		 */	
		return true;
	}


	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch ( item.getItemId()){
		case 1:
			Intent intent1= new Intent(getApplicationContext(), MainActivity.class);
			startActivity(intent1);

			Toast.makeText(this, "로그인 화면 가기", Toast.LENGTH_SHORT).show();
			return true;

		case 2 :
			Intent intent2= new Intent(getApplicationContext(), DreaminsertActivity.class);
			startActivity(intent2);
			Toast.makeText(this, "나의 꿈 등록 하기", Toast.LENGTH_SHORT).show();
			return true;

		case 3 :
			Intent intent3= new Intent(getApplicationContext(), DreamMap.class);
			startActivity(intent3);

			Toast.makeText(this, "나의 꿈 지도 보기", Toast.LENGTH_SHORT).show();
			return true;

		case 4 :
			Intent intent4= new Intent(getApplicationContext(), com.bit.dreamer.SubMenu.class);
			startActivity(intent4);
			Toast.makeText(this , "다양한 메뉴들 보기", Toast.LENGTH_SHORT).show();
			return true;

//		case 5 :    // 축소로 주석
//			Intent intent5= new Intent(getApplicationContext(), PrologueActivity.class);
//			startActivity(intent5);
//			Toast.makeText(this, "도움말", Toast.LENGTH_SHORT).show();
//			return true;
//			
//		case 6 :
//			Intent intent6= new Intent(getApplicationContext(), PsycholTestActivity.class);
//			startActivity(intent6);
//			Toast.makeText(this, "심리테스트", Toast.LENGTH_SHORT).show();
//			return true;
//			
//		case 7 :
//			Intent intent7= new Intent(getApplicationContext(), DreamMap.class);
//			startActivity(intent7);
//			Toast.makeText(this, "심리테스트", Toast.LENGTH_SHORT).show();
//			return true;

		}
		
		return false;

	}

}
